#!/usr/bin/env python
#-*- coding: utf-8 -*-

import gtk, gobject, graphoid, config, routeList

class Params:

    def __init__(self, tree):
        
        self.selection = None
        self.tree = tree
        
        def getFieldValue(column, renderer, model, it, data=None):
            field = model.get_value(it, 1)
            obj = self.selection
            if obj != None:
                if obj.__class__ == routeList.routeList:
                    values = list(set([getattr(r, field) for r in obj]))
                    if len(values) == 1:
                        text = str(values[0])
                    else:
                        text = u'rożne wartości'
                else:
                    text = str(getattr(obj, field))
            else:
                text = ''
            renderer.set_property('text', text) 
                
        def handEdit(renderer, path, text, data=None):
            model = self.tree.get_model()
            field = model[path][1]
            value = model[path][2](text)
            obj = self.selection
            if obj != None:
                if obj.__class__ == routeList.routeList:
                    for r in obj:
                        setattr(r, field, value)
                else:
                    setattr(obj, field, value)
                        
        self.nodeParamsLS = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, object)
        self.edgeParamsLS = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, object)
        self.routeParamsLS = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, object)
        self.configParamsLS = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, object)
        self.routeListParamsLS = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, object)

        """Dodawanie modyfikowalnych parametrow.
        Do odpowiedniego ListStore (do wyboru node, edge, route i config)
        nalezy appendowac krotkę: 
        (opis w GUI, nazwa pola obiektu, konstruktor obiektu ze stringa).
        """ 
        self.nodeParamsLS.append(('safe node', 'isSafeNode', lambda x: x in ['True', '1']))
        self.nodeParamsLS.append(('lights', 'hasLights', lambda x: x in ['True', '1']))
        self.nodeParamsLS.append((u'zielone światła', 'greenLightPhase', float))
        self.nodeParamsLS.append((u'czerwone światła', 'redLightPhase', float))
        self.nodeParamsLS.append(('lights start', 'startEps', float))
        self.nodeParamsLS.append(('ID', 'ID',  str))
        self.nodeParamsLS.append(('priorytet', 'priority',  int))
                                                        
        self.edgeParamsLS.append(('priorytet', 'priority', int))
        
        self.routeParamsLS.append((u'route μ', 'routeMu', float))        
        self.routeParamsLS.append((u'route σ', 'routeSigma', float))        
                
        self.configParamsLS.append((u'route start μ', 'routeStartMu', float))
        self.configParamsLS.append((u'route start σ', 'routeStartSigma', float))
      
        self.routeListParamsLS.append((u'route μ', 'routeMu', float))        
        self.routeListParamsLS.append((u'route σ', 'routeSigma', float))       
        
        paramRenderer = gtk.CellRendererText()
        valueRenderer = gtk.CellRendererText()        
        valueRenderer.set_property('editable', True)
        valueRenderer.connect('edited', handEdit)
            
        paramColumn = gtk.TreeViewColumn(u'Parametr', paramRenderer, text=0)
        valueColumn = gtk.TreeViewColumn(u'Wartość', valueRenderer) 
        valueColumn.set_cell_data_func(valueRenderer, getFieldValue)
        
        self.tree.append_column(paramColumn)
        self.tree.append_column(valueColumn)          
    
    def setSelection(self, sel=None):
        self.selection = None
        
        if sel == None:
            model = None
        elif sel.__class__ == graphoid.basicNode:
            model = self.nodeParamsLS
        elif sel.__class__ == graphoid.basicEdge:
            model = self.edgeParamsLS
        elif sel.__class__ == graphoid.basicRoute:
            model = self.routeParamsLS
        elif sel == config:
            model = self.configParamsLS
        elif sel.__class__ == routeList.routeList:
            model = self.routeListParamsLS
            
        self.tree.set_model(model)
        self.selection = sel        
        self.tree.queue_draw()  